package com.puman.beautyfarmcounselor;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.widget.Toast;


import com.puman.beautyfarmcounselor.ui.setting.LoginFragment_;
import com.puman.beautyfarmcounselor.util.loading.Loading;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;

@EActivity(R.layout.index_main)
public class IndexActivity extends FragmentActivity {

    static FragmentManager fragmentManager;

    static IndexActivity indexActivity;

    public static IndexActivity getInstace() {
        return indexActivity;
    }

    @AfterViews
    void afterView(){
        indexActivity = this;
        Loading.context = this;
        fragmentManager = getSupportFragmentManager();
        getSupportFragmentManager()
                .beginTransaction().replace(R.id.index_container, new LoginFragment_()).commit();
//        doInBackground();
    }

    @Background(delay = 0)
    void doInBackground(){
        Intent intent = new Intent();
        intent.setClass(IndexActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }


    private boolean is2CallBack = false;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (fragmentManager.getBackStackEntryCount() != 0) {
                fragmentManager.popBackStack();
            } else {
                if (!is2CallBack) {
                    is2CallBack = true;
                    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            is2CallBack = false;
                        }
                    }, 1500);
                } else {
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            }
        }
        return true;
    }

}
